//
//  HomeTableViewController.swift
//  weibo
//
//  Created by yellow on 15/10/14.
//  Copyright © 2015年 yellow. All rights reserved.
//

import UIKit
import SVProgressHUD

/// 原创微博Cell的可重用标示符
let StatusNormalCellID = "StatusNormalCellID"
/// 转发微博Cell的可重用标识符
let RelayStatusViewCellID = "RelayStatusViewCellID"

class HomeTableViewController: VisitTableViewController {

    let viewModel = StatusViewModel.sharedViewModel



    override func viewDidLoad() {
        super.viewDidLoad()
        if !UserAccountViewModel.sharedViewModel.isLoginUser {
            myVisiterView?.setInfo(nil, tips: "关注一些人，回这里看看有什么惊喜")
            return
        }
        
        // TODO:cell类型
        tableView.registerClass(StatusNormalCell.self, forCellReuseIdentifier: StatusNormalCellID)
        tableView.registerClass(RelayStatusViewCell.self, forCellReuseIdentifier: RelayStatusViewCellID)
        //设置刷新视图,下拉刷新控件默认没有 - 高度 60
        refreshControl = WBRefreshControl()
        refreshControl!.addTarget(self, action: "loadData", forControlEvents: UIControlEvents.ValueChanged)
        
        //上拉刷新view
        tableView.tableFooterView = pullUpView
        
        //设置预估行高，根据微博正文计算行高
        tableView.estimatedRowHeight = 400
        
        //第一进入时加载数据
        loadData()
        
        //添加对StatusPictureViewCellSelectedNotification通知的监听,(此处有循环引用)
        NSNotificationCenter.defaultCenter().addObserverForName(StatusPictureViewCellSelectedNotification, object: nil, queue: nil) {[weak self] (notification) -> Void in
<<<<<<< HEAD
            guard let indexPath = notification.userInfo?[StatusPictureViewCellSelectedIndexPathKey] as? NSIndexPath else{
                return
            }
            
            guard let phtotoURLS = notification.userInfo?[StatusPictureViewCellSelectedURLKey] as? [NSURL] else{
=======
            guard let notiDict = notification.userInfo as? [String:AnyObject] else{
>>>>>>> 118ca216dc87274a8bb57ab1f83f49fa257469d4
                return
            }
            
            //接收到点击配图cell的info传递给modal出来的PhotoBrowserViewController
<<<<<<< HEAD
            self?.presentViewController(PhotoBrowserViewController(selectedIndexPath: indexPath, photoURLS: phtotoURLS), animated: true, completion: nil)
=======
            self?.presentViewController(PhotoBrowserViewController(photoInfoDict: notiDict), animated: true, completion: nil)
>>>>>>> 118ca216dc87274a8bb57ab1f83f49fa257469d4
        }
    }
    
/// 析构函数
    deinit{
/// 注销通知
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    //下拉时会触发加载数据
    @objc private func loadData(){
        refreshControl!.beginRefreshing()
        viewModel.loadStatus(isPullUp: pullUpView.isAnimating()) { (isSuccess) -> () in
            self.refreshControl!.endRefreshing()
            // 关闭上拉刷新
            self.pullUpView.stopAnimating()
            if isSuccess {
                self.tableView.reloadData()
            }else{
                SVProgressHUD.showInfoWithStatus("网络有问题")
            }
        }

    }
    
    //懒加载上拉刷控件
    private var pullUpView: UIActivityIndicatorView = {
        let pullView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        pullView.color = UIColor.darkGrayColor()
        return pullView
    }()

  }

// MARK: - tableview的数据源
extension HomeTableViewController{
    
    

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.statusList.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let statusDetailViewModel = viewModel.statusList[indexPath.row]
        let cellID = statusDetailViewModel.cellId
        
        //将cell转为基类的cell，因为我们只需要使用基类中的viewModel
        let cell = tableView.dequeueReusableCellWithIdentifier(cellID!, forIndexPath: indexPath) as! StatusViewCell
        
        //传递StatusDetailViewModel到StatusViewCell中，StatusViewCell再分发到topView或是BottomView中
        cell.viewModel = statusDetailViewModel
        
        if indexPath.row == viewModel.statusList.count - 1 && !pullUpView.isAnimating(){
            //当到最后一个微博时，开始下拉刷新数据
            pullUpView.startAnimating()
            loadData()
        }
        return cell
    }
}


// MARK: - tableview的代理
extension HomeTableViewController{
    
    //获取当前cell对应的viewmodel，并调用cell的获取cell高度的方法
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return viewModel.statusList[indexPath.row].rowHight
    }
    
}

